package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/13.
 */

public class AlterUserNaneActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.tv_submit)
    TextView tv_submit;
    @Bind(R.id.ev_text)
    AppCompatEditText ev_text;
    private final String ALTER_USER_NANE = "AlterUserNaneActivity";
    private final String MINE_FRAGMENT = "MineFragment";
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activiity_alter_user_nane);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        loadingUtil = new LoadingUtil(this);
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "修改用户名");
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }


    @Override
    public void onStop() {
        loadingUtil.stopShowLoading();
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case ALTER_USER_NANE:
                loadingUtil.stopShowLoading();
                if (jsonObject != null) {
                    if (jsonObject.optString("message").equals("修改成功")) {
                        ServiceUrl.UserInfoService(this, MINE_FRAGMENT);
                        Intent intent = new Intent();
                        intent.putExtra("nickname", ev_text.getText().toString());
                        setResult(1, intent);
                        finish_Activity();
                    }
                }
                break;
        }
    }

    @OnClick({R.id.tv_submit})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_submit:
                if (!ev_text.getText().toString().equals("") && ev_text.getText().toString() != null) {
                    loadingUtil.startShowLoading();
                    ServiceUrl.UpdaTenameService(this, ev_text.getText().toString(), ALTER_USER_NANE);
                } else {
                    UtilTool.toast(this, "用户名不能为空");
                }
                break;
        }
    }
}
